setwd("")

##This code replicates the analyses in Appendix A. Email cdmyers@umn.edu with questions.
##The input files are the long version of the dataset and, later on, a csv that has the category codes assigned to descriptors.

library(epiDisplay)
library(tidyr)

long <- read.csv("Combined data_long.csv")

long$GenD_recode <- strtrim(long$GenD_recode, 30)
long$GenR_recode <- strtrim(long$GenR_recode, 30)

long$GenD_recode[which(long$GenD_recode == "wealth")] <- "rich"
long$GenR_recode[which(long$GenR_recode == "wealth")] <- "rich"

long$GenD_recode[which(long$GenD_recode == "hard working")] <- "hardworking"
long$GenR_recode[which(long$GenR_recode == "hard working")] <- "hardworking"

long$GenD_recode[which(long$GenD_recode == "nothing")] <- "[NONRESPONSE]"
long$GenR_recode[which(long$GenR_recode == "nothing")] <- "[NONRESPONSE]"


##Sample 1 is 2016, sample 2 is 2021.

##Now generating a list of all descriptors that appear at least 10 times across both samples

#Dems first

demDescriptors <- names(tab1(long$GenD_recode, sort.group = "decreasing", graph = FALSE, cum.percent = FALSE)$output.table[which(tab1(long$GenD_recode, sort.group = "decreasing", graph = FALSE, cum.percent = FALSE)$output.table[,1] > 9),1])
repDescriptors <- names(tab1(long$GenR_recode, sort.group = "decreasing", graph = FALSE, cum.percent = FALSE)$output.table[which(tab1(long$GenR_recode, sort.group = "decreasing", graph = FALSE, cum.percent = FALSE)$output.table[,1] > 9),1])

demDescriptorsCount <- rep(0,length(demDescriptors))
demDescriptorsCountSample1 <- rep(0,length(demDescriptors))
demDescriptorsCountSample2 <- rep(0,length(demDescriptors))
demDescriptorsCountSample1Dems <- rep(0,length(demDescriptors))
demDescriptorsCountSample2Dems <- rep(0,length(demDescriptors))
demDescriptorsCountSample1Reps <- rep(0,length(demDescriptors))
demDescriptorsCountSample2Reps <- rep(0,length(demDescriptors))

#Creating a new wide dataset from the long dataset to list all descriptors from a single respondent

long$demWordNum <- rep(c("dem1","dem2","dem3","dem4"), (length(long$ID)/4))

wideFromLongDem <- pivot_wider(subset(long, select = -c(GenD, GenR, GenR_v, GenR_recode)), names_from = demWordNum, values_from = c(GenD_recode, GenD_v))

colnames(wideFromLongDem)[15:22] <- c("dem1", "dem2", "dem3", "dem4", "dem1_v", "dem2_v", "dem3_v", "dem4_v")


##This next code loops through the whole list of descriptors of Dems and records the number of people who used each descriptor in each sample and in partisan subsamples

for (i in 1:length(demDescriptors)) {
  demDescriptorsCount[i] <- sum(wideFromLongDem$dem1 == demDescriptors[i] | wideFromLongDem$dem2 == demDescriptors[i] | wideFromLongDem$dem3 == demDescriptors[i] | wideFromLongDem$dem4 == demDescriptors[i], na.rm = T)
  
  demDescriptorsCountSample1[i] <- sum(wideFromLongDem$dem1[which(wideFromLongDem$sample == 1)] == demDescriptors[i] | wideFromLongDem$dem2[which(wideFromLongDem$sample == 1)] == demDescriptors[i] | wideFromLongDem$dem3[which(wideFromLongDem$sample == 1)] == demDescriptors[i] | wideFromLongDem$dem4[which(wideFromLongDem$sample == 1)] == demDescriptors[i], na.rm = T)
  
  demDescriptorsCountSample2[i] <- sum(wideFromLongDem$dem1[which(wideFromLongDem$sample == 2)] == demDescriptors[i] | wideFromLongDem$dem2[which(wideFromLongDem$sample == 2)] == demDescriptors[i] | wideFromLongDem$dem3[which(wideFromLongDem$sample == 2)] == demDescriptors[i] | wideFromLongDem$dem4[which(wideFromLongDem$sample == 2)] == demDescriptors[i], na.rm = T)
  
  demDescriptorsCountSample1Dems[i] <- sum(wideFromLongDem$dem1[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 1)] == demDescriptors[i] | wideFromLongDem$dem2[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 1)] == demDescriptors[i] | wideFromLongDem$dem3[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 1)] == demDescriptors[i] | wideFromLongDem$dem4[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 1)] == demDescriptors[i], na.rm = T)
  
  demDescriptorsCountSample2Dems[i] <- sum(wideFromLongDem$dem1[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 1)] == demDescriptors[i] | wideFromLongDem$dem2[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 1)] == demDescriptors[i] | wideFromLongDem$dem3[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 1)] == demDescriptors[i] | wideFromLongDem$dem4[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 1)] == demDescriptors[i], na.rm = T)
  
  demDescriptorsCountSample1Reps[i] <- sum(wideFromLongDem$dem1[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 3)] == demDescriptors[i] | wideFromLongDem$dem2[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 3)] == demDescriptors[i] | wideFromLongDem$dem3[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 3)] == demDescriptors[i] | wideFromLongDem$dem4[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 3)] == demDescriptors[i], na.rm = T)
  
  demDescriptorsCountSample2Reps[i] <- sum(wideFromLongDem$dem1[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 3)] == demDescriptors[i] | wideFromLongDem$dem2[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 3)] == demDescriptors[i] | wideFromLongDem$dem3[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 3)] == demDescriptors[i] | wideFromLongDem$dem4[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 3)] == demDescriptors[i], na.rm = T)
}

##Merging this all into a data frame for analysis

descriptorsOfDems <- data.frame(demDescriptors, demDescriptorsCount, demDescriptorsCountSample1, demDescriptorsCountSample2, demDescriptorsCountSample1Dems, demDescriptorsCountSample2Dems, demDescriptorsCountSample1Reps, demDescriptorsCountSample2Reps)

descriptorsOfDems$prop.test_All <- 1
descriptorsOfDems$prop.test_Dems <- 1
descriptorsOfDems$prop.test_Reps <- 1

##Looping a proposition test that, for each descriptor, compares the proportion of respondents, Dem respondents, and Rep respondents who use the descriptor in each sample

for (i in c(1:length(descriptorsOfDems$demDescriptors))) {
  descriptorsOfDems$prop.test_All[i] <- prop.test(c(descriptorsOfDems$demDescriptorsCountSample1[i], descriptorsOfDems$demDescriptorsCountSample2[i]), c(length(wideFromLongDem$sample[which(wideFromLongDem$sample == 1)]), length(wideFromLongDem$sample[which(wideFromLongDem$sample == 2)])))$p.value
}

for (i in c(1:length(descriptorsOfDems$demDescriptors))) {
  descriptorsOfDems$prop.test_Dems[i] <- prop.test(c(descriptorsOfDems$demDescriptorsCountSample1Dems[i], descriptorsOfDems$demDescriptorsCountSample2Dems[i]), c(length(wideFromLongDem$sample[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 1)]), length(wideFromLongDem$sample[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 1)])))$p.value
}

for (i in c(1:length(descriptorsOfDems$demDescriptors))) {
  descriptorsOfDems$prop.test_Reps[i] <- prop.test(c(descriptorsOfDems$demDescriptorsCountSample1Reps[i], descriptorsOfDems$demDescriptorsCountSample2Reps[i]), c(length(wideFromLongDem$sample[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 3)]), length(wideFromLongDem$sample[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 3)])))$p.value
}

##Couning number of respondents and calculating proportions

descriptorsOfDems$sample2N <-length(wideFromLongDem$sample[which(wideFromLongDem$sample == 2 )])
descriptorsOfDems$sample1N <-length(wideFromLongDem$sample[which(wideFromLongDem$sample == 1)])
descriptorsOfDems$demSample2N <-length(wideFromLongDem$sample[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 1)])
descriptorsOfDems$demSample1N <-length(wideFromLongDem$sample[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 1)])
descriptorsOfDems$repSample2N <-length(wideFromLongDem$sample[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 3)])
descriptorsOfDems$repSample1N <-length(wideFromLongDem$sample[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 3)])

descriptorsOfDems$sample1Prop <- descriptorsOfDems$demDescriptorsCountSample1/descriptorsOfDems$sample1N
descriptorsOfDems$sample2Prop <- descriptorsOfDems$demDescriptorsCountSample2/descriptorsOfDems$sample2N
descriptorsOfDems$demSample1Prop <- descriptorsOfDems$demDescriptorsCountSample1Dems/descriptorsOfDems$demSample1N
descriptorsOfDems$demSmple2Prop <- descriptorsOfDems$demDescriptorsCountSample2Dems/descriptorsOfDems$demSample2N
descriptorsOfDems$repSample1Prop <- descriptorsOfDems$demDescriptorsCountSample1Reps/descriptorsOfDems$repSample1N
descriptorsOfDems$repSample2Prop <- descriptorsOfDems$demDescriptorsCountSample2Reps/descriptorsOfDems$repSample2N

##Adding an indicator to flag words where there is a significant difference overall or in at least one sub-sample

descriptorsOfDems$sig <- ifelse((descriptorsOfDems$prop.test_All < .05 | descriptorsOfDems$prop.test_Dems < .05 | descriptorsOfDems$prop.test_Reps < .05),1,0)

##Finally, writing this all to a CSV file. Code later in this file will repliate the exact tables that appear in Appendix A.

write.csv(descriptorsOfDems, "descriptorsOfDems.csv", row.names = FALSE)




#now descriptors of Reps

repDescriptors <- names(tab1(long$GenR_recode, sort.group = "decreasing", graph = FALSE, cum.percent = FALSE)$output.table[which(tab1(long$GenR_recode, sort.group = "decreasing", graph = FALSE, cum.percent = FALSE)$output.table[,1] > 9),1])
repDescriptors <- names(tab1(long$GenR_recode, sort.group = "decreasing", graph = FALSE, cum.percent = FALSE)$output.table[which(tab1(long$GenR_recode, sort.group = "decreasing", graph = FALSE, cum.percent = FALSE)$output.table[,1] > 9),1])

repDescriptorsCount <- rep(0,length(repDescriptors))
repDescriptorsCountSample1 <- rep(0,length(repDescriptors))
repDescriptorsCountSample2 <- rep(0,length(repDescriptors))
repDescriptorsCountSample1Dems <- rep(0,length(repDescriptors))
repDescriptorsCountSample2Dems <- rep(0,length(repDescriptors))
repDescriptorsCountSample1Reps <- rep(0,length(repDescriptors))
repDescriptorsCountSample2Reps <- rep(0,length(repDescriptors))

#Creating a new wide dataset from the long dataset to hold each word in a different cell

long$repWordNum <- rep(c("rep1","rep2","rep3","rep4"), (length(long$ID)/4))

wideFromLongRep <- pivot_wider(subset(long, select = -c(demWordNum, GenD, GenR, GenD_v, GenD_recode)), names_from = repWordNum, values_from = c(GenR_recode, GenR_v))

colnames(wideFromLongRep)[15:22] <- c("rep1","rep2","rep3","rep4", "rep1_v", "rep2_v", "rep3_v", "rep4_v")

for (i in 1:length(repDescriptors)) {
  repDescriptorsCount[i] <- sum(wideFromLongRep$rep1 == repDescriptors[i] | wideFromLongRep$rep2 == repDescriptors[i] | wideFromLongRep$rep3 == repDescriptors[i] | wideFromLongRep$rep4 == repDescriptors[i], na.rm = T)
  
  repDescriptorsCountSample1[i] <- sum(wideFromLongRep$rep1[which(wideFromLongRep$sample == 1)] == repDescriptors[i] | wideFromLongRep$rep2[which(wideFromLongRep$sample == 1)] == repDescriptors[i] | wideFromLongRep$rep3[which(wideFromLongRep$sample == 1)] == repDescriptors[i] | wideFromLongRep$rep4[which(wideFromLongRep$sample == 1)] == repDescriptors[i], na.rm = T)
  
  repDescriptorsCountSample2[i] <- sum(wideFromLongRep$rep1[which(wideFromLongRep$sample == 2)] == repDescriptors[i] | wideFromLongRep$rep2[which(wideFromLongRep$sample == 2)] == repDescriptors[i] | wideFromLongRep$rep3[which(wideFromLongRep$sample == 2)] == repDescriptors[i] | wideFromLongRep$rep4[which(wideFromLongRep$sample == 2)] == repDescriptors[i], na.rm = T)
  
  repDescriptorsCountSample1Dems[i] <- sum(wideFromLongRep$rep1[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 1)] == repDescriptors[i] | wideFromLongRep$rep2[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 1)] == repDescriptors[i] | wideFromLongRep$rep3[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 1)] == repDescriptors[i] | wideFromLongRep$rep4[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 1)] == repDescriptors[i], na.rm = T)
  
  repDescriptorsCountSample2Dems[i] <- sum(wideFromLongRep$rep1[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 1)] == repDescriptors[i] | wideFromLongRep$rep2[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 1)] == repDescriptors[i] | wideFromLongRep$rep3[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 1)] == repDescriptors[i] | wideFromLongRep$rep4[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 1)] == repDescriptors[i], na.rm = T)
  
  repDescriptorsCountSample1Reps[i] <- sum(wideFromLongRep$rep1[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 3)] == repDescriptors[i] | wideFromLongRep$rep2[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 3)] == repDescriptors[i] | wideFromLongRep$rep3[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 3)] == repDescriptors[i] | wideFromLongRep$rep4[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 3)] == repDescriptors[i], na.rm = T)
  
  repDescriptorsCountSample2Reps[i] <- sum(wideFromLongRep$rep1[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 3)] == repDescriptors[i] | wideFromLongRep$rep2[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 3)] == repDescriptors[i] | wideFromLongRep$rep3[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 3)] == repDescriptors[i] | wideFromLongRep$rep4[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 3)] == repDescriptors[i], na.rm = T)
}

descriptorsOfReps <- data.frame(repDescriptors, repDescriptorsCount, repDescriptorsCountSample1, repDescriptorsCountSample2, repDescriptorsCountSample1Dems, repDescriptorsCountSample2Dems, repDescriptorsCountSample1Reps, repDescriptorsCountSample2Reps)

descriptorsOfReps$prop.test_All <- 1
descriptorsOfReps$prop.test_Dems <- 1
descriptorsOfReps$prop.test_Reps <- 1


for (i in c(1:length(descriptorsOfReps$repDescriptors))) {
  descriptorsOfReps$prop.test_All[i] <- prop.test(c(descriptorsOfReps$repDescriptorsCountSample1[i], descriptorsOfReps$repDescriptorsCountSample2[i]), c(length(wideFromLongRep$sample[which(wideFromLongRep$sample == 1)]), length(wideFromLongRep$sample[which(wideFromLongRep$sample == 2)])))$p.value
}

for (i in c(1:length(descriptorsOfReps$repDescriptors))) {
  descriptorsOfReps$prop.test_Dems[i] <- prop.test(c(descriptorsOfReps$repDescriptorsCountSample1Dems[i], descriptorsOfReps$repDescriptorsCountSample2Dems[i]), c(length(wideFromLongRep$sample[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 1)]), length(wideFromLongRep$sample[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 1)])))$p.value
}

for (i in c(1:length(descriptorsOfReps$repDescriptors))) {
  descriptorsOfReps$prop.test_Reps[i] <- prop.test(c(descriptorsOfReps$repDescriptorsCountSample1Reps[i], descriptorsOfReps$repDescriptorsCountSample2Reps[i]), c(length(wideFromLongRep$sample[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 3)]), length(wideFromLongRep$sample[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 3)])))$p.value
}

descriptorsOfReps$sample2N <-length(wideFromLongRep$sample[which(wideFromLongRep$sample == 2 )])
descriptorsOfReps$sample1N <-length(wideFromLongRep$sample[which(wideFromLongRep$sample == 1)])
descriptorsOfReps$demSample2N <-length(wideFromLongRep$sample[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 1)])
descriptorsOfReps$demSample1N <-length(wideFromLongRep$sample[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 1)])
descriptorsOfReps$repSample2N <-length(wideFromLongRep$sample[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 3)])
descriptorsOfReps$repSample1N <-length(wideFromLongRep$sample[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 3)])

descriptorsOfReps$sample1Prop <- descriptorsOfReps$repDescriptorsCountSample1/descriptorsOfReps$sample1N
descriptorsOfReps$sample2Prop <- descriptorsOfReps$repDescriptorsCountSample2/descriptorsOfReps$sample2N
descriptorsOfReps$demSample1Prop <- descriptorsOfReps$repDescriptorsCountSample1Dems/descriptorsOfReps$demSample1N
descriptorsOfReps$demSmple2Prop <- descriptorsOfReps$repDescriptorsCountSample2Dems/descriptorsOfReps$demSample2N
descriptorsOfReps$repSample1Prop <- descriptorsOfReps$repDescriptorsCountSample1Reps/descriptorsOfReps$repSample1N
descriptorsOfReps$repSample2Prop <- descriptorsOfReps$repDescriptorsCountSample2Reps/descriptorsOfReps$repSample2N


descriptorsOfReps$sig <- ifelse((descriptorsOfReps$prop.test_All < .05 | descriptorsOfReps$prop.test_Dems < .05 | descriptorsOfReps$prop.test_Reps < .05),1,0)

write.csv(descriptorsOfReps, "descriptorsOfReps.csv", row.names = FALSE)

##Now adding code to apply codes from Myers open-ended analysis to word lists

##Reading in the codes applied to each descriptor from inductive coding of 2016 data

adjectiveCodes <- read.csv("2016AdjectiveCoding.csv")

adjectiveCodes <- subset(adjectiveCodes, select = -c(Notes))

## Attaching these codes to the long dataset and then create two new datasets that turn this new long dataset into a wide dataset with a column for each word used by the subject and a separate column holding the coding of that word

colnames(adjectiveCodes) <- c("GenD_recode","demCategory")

longAdjectiveCodes <- merge(long, adjectiveCodes, by = c("GenD_recode"), all.x = TRUE)

colnames(adjectiveCodes) <- c("GenR_recode","repCategory")

longAdjectiveCodes <- merge(longAdjectiveCodes, adjectiveCodes, by = c("GenR_recode"), all.x = TRUE)

colnames(adjectiveCodes) <- c("dem1","dem1Category")

wideFromLongDemAdjectiveCodes <- merge(wideFromLongDem, adjectiveCodes, by = c("dem1"), all.x = TRUE)

colnames(adjectiveCodes) <- c("dem2","dem2Category")

wideFromLongDemAdjectiveCodes <- merge(wideFromLongDemAdjectiveCodes, adjectiveCodes, by = c("dem2"), all.x = TRUE)

colnames(adjectiveCodes) <- c("dem3","dem3Category")

wideFromLongDemAdjectiveCodes <- merge(wideFromLongDemAdjectiveCodes, adjectiveCodes, by = c("dem3"), all.x = TRUE)

colnames(adjectiveCodes) <- c("dem4","dem4Category")

wideFromLongDemAdjectiveCodes <- merge(wideFromLongDemAdjectiveCodes, adjectiveCodes, by = c("dem4"), all.x = TRUE)


colnames(adjectiveCodes) <- c("rep1","rep1Category")

wideFromLongRepAdjectiveCodes <- merge(wideFromLongRep, adjectiveCodes, by = c("rep1"), all.x = TRUE)

colnames(adjectiveCodes) <- c("rep2","rep2Category")

wideFromLongRepAdjectiveCodes <- merge(wideFromLongRepAdjectiveCodes, adjectiveCodes, by = c("rep2"), all.x = TRUE)

colnames(adjectiveCodes) <- c("rep3","rep3Category")

wideFromLongRepAdjectiveCodes <- merge(wideFromLongRepAdjectiveCodes, adjectiveCodes, by = c("rep3"), all.x = TRUE)

colnames(adjectiveCodes) <- c("rep4","rep4Category")

wideFromLongRepAdjectiveCodes <- merge(wideFromLongRepAdjectiveCodes, adjectiveCodes, by = c("rep4"), all.x = TRUE)

##Then, the remaining code  replicates the procedure used above to count and compare the proportion of respondents in each sample who uses each descriptor, except here we are counting and comparing the proportion of respondents who use at least one descriptor from each category 

demCategories <- names(table(adjectiveCodes$rep4Category))

repCategories <- demCategories


demCategoriesCount <- rep(0,length(demCategories))
demCategoriesCountSample1 <- rep(0,length(demCategories))
demCategoriesCountSample2 <- rep(0,length(demCategories))
demCategoriesCountSample1Dems <- rep(0,length(demCategories))
demCategoriesCountSample2Dems <- rep(0,length(demCategories))
demCategoriesCountSample1Reps <- rep(0,length(demCategories))
demCategoriesCountSample2Reps <- rep(0,length(demCategories))

for (i in 1:length(demCategories)) {
  demCategoriesCount[i] <- sum(wideFromLongDemAdjectiveCodes$dem1Category == demCategories[i] | wideFromLongDemAdjectiveCodes$dem2Category == demCategories[i] | wideFromLongDemAdjectiveCodes$dem3Category == demCategories[i] | wideFromLongDemAdjectiveCodes$dem4Category == demCategories[i], na.rm = T)
  
  demCategoriesCountSample1[i] <- sum(wideFromLongDemAdjectiveCodes$dem1Category[which(wideFromLongDemAdjectiveCodes$sample == 1)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem2Category[which(wideFromLongDemAdjectiveCodes$sample == 1)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem3Category[which(wideFromLongDemAdjectiveCodes$sample == 1)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem4Category[which(wideFromLongDemAdjectiveCodes$sample == 1)] == demCategories[i],na.rm = T)
  
  demCategoriesCountSample2[i] <- sum(wideFromLongDemAdjectiveCodes$dem1Category[which(wideFromLongDemAdjectiveCodes$sample == 2)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem2Category[which(wideFromLongDemAdjectiveCodes$sample == 2)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem3Category[which(wideFromLongDemAdjectiveCodes$sample == 2)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem4Category[which(wideFromLongDemAdjectiveCodes$sample == 2)] == demCategories[i], na.rm = T)
  
  demCategoriesCountSample1Dems[i] <- sum(wideFromLongDemAdjectiveCodes$dem1Category[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem2Category[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem3Category[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem4Category[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)] == demCategories[i], na.rm = T)
  
  demCategoriesCountSample2Dems[i] <- sum(wideFromLongDemAdjectiveCodes$dem1Category[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem2Category[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem3Category[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem4Category[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)] == demCategories[i], na.rm = T)
  
  demCategoriesCountSample1Reps[i] <- sum(wideFromLongDemAdjectiveCodes$dem1Category[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem2Category[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem3Category[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem4Category[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)] == demCategories[i], na.rm = T)
  
  demCategoriesCountSample2Reps[i] <- sum(wideFromLongDemAdjectiveCodes$dem1Category[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem2Category[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem3Category[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)] == demCategories[i] | wideFromLongDemAdjectiveCodes$dem4Category[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)] == demCategories[i], na.rm = T)
}

categoriesOfDems <- data.frame(demCategories, demCategoriesCount, demCategoriesCountSample1, demCategoriesCountSample2, demCategoriesCountSample1Dems, demCategoriesCountSample2Dems, demCategoriesCountSample1Reps, demCategoriesCountSample2Reps)

categoriesOfDems$prop.test_All <- 1
categoriesOfDems$prop.test_Dems <- 1
categoriesOfDems$prop.test_Reps <- 1


for (i in c(1:length(categoriesOfDems$demCategories))) {
  categoriesOfDems$prop.test_All[i] <- prop.test(c(categoriesOfDems$demCategoriesCountSample1[i], categoriesOfDems$demCategoriesCountSample2[i]), c(length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 1)]), length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 2)])))$p.value
}

for (i in c(1:length(categoriesOfDems$demCategories))) {
  categoriesOfDems$prop.test_Dems[i] <- prop.test(c(categoriesOfDems$demCategoriesCountSample1Dems[i], categoriesOfDems$demCategoriesCountSample2Dems[i]), c(length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)]), length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)])))$p.value
}

for (i in c(1:length(categoriesOfDems$demCategories))) {
  categoriesOfDems$prop.test_Reps[i] <- prop.test(c(categoriesOfDems$demCategoriesCountSample1Reps[i], categoriesOfDems$demCategoriesCountSample2Reps[i]), c(length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)]), length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)])))$p.value
}

categoriesOfDems$sample2N <-length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 2 )])
categoriesOfDems$sample1N <-length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 1)])
categoriesOfDems$demSample2N <-length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)])
categoriesOfDems$demSample1N <-length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)])
categoriesOfDems$repSample2N <-length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)])
categoriesOfDems$repSample1N <-length(wideFromLongDemAdjectiveCodes$sample[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)])

categoriesOfDems$sample1Prop <- categoriesOfDems$demCategoriesCountSample1/categoriesOfDems$sample1N
categoriesOfDems$sample2Prop <- categoriesOfDems$demCategoriesCountSample2/categoriesOfDems$sample2N
categoriesOfDems$demSample1Prop <- categoriesOfDems$demCategoriesCountSample1Dems/categoriesOfDems$demSample1N
categoriesOfDems$demSmple2Prop <- categoriesOfDems$demCategoriesCountSample2Dems/categoriesOfDems$demSample2N
categoriesOfDems$repSample1Prop <- categoriesOfDems$demCategoriesCountSample1Reps/categoriesOfDems$repSample1N
categoriesOfDems$repSample2Prop <- categoriesOfDems$demCategoriesCountSample2Reps/categoriesOfDems$repSample2N


categoriesOfDems$sig <- ifelse((categoriesOfDems$prop.test_All < .05 | categoriesOfDems$prop.test_Dems < .05 | categoriesOfDems$prop.test_Reps < .05),1,0)

write.csv(categoriesOfDems, "categoriesOfDems.csv", row.names = FALSE)

#Now categorizing respondents' `type' by their most used kind of descriptor. Respondents whose used the same number of descriptors from multiple categories (e.g. two personal traits and two ideological descriptors) are coded as both types

wideFromLongDemAdjectiveCodes$PT_number <- ifelse(is.na(wideFromLongDemAdjectiveCodes$dem1Category) == T, F,  wideFromLongDemAdjectiveCodes$dem1Category == "Personal trait ") + 
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem2Category) == T, F,  wideFromLongDemAdjectiveCodes$dem2Category == "Personal trait ") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem3Category) == T, F,  wideFromLongDemAdjectiveCodes$dem3Category == "Personal trait ") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem4Category) == T, F,  wideFromLongDemAdjectiveCodes$dem4Category == "Personal trait ")

wideFromLongDemAdjectiveCodes$Value_number <- ifelse(is.na(wideFromLongDemAdjectiveCodes$dem1Category) == T, F,  wideFromLongDemAdjectiveCodes$dem1Category == "Value") + 
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem2Category) == T, F,  wideFromLongDemAdjectiveCodes$dem2Category == "Value") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem3Category) == T, F,  wideFromLongDemAdjectiveCodes$dem3Category == "Value") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem4Category) == T, F,  wideFromLongDemAdjectiveCodes$dem4Category == "Value")

wideFromLongDemAdjectiveCodes$Ideology_number <- ifelse(is.na(wideFromLongDemAdjectiveCodes$dem1Category) == T, F,  wideFromLongDemAdjectiveCodes$dem1Category == "Ideology ") + 
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem2Category) == T, F,  wideFromLongDemAdjectiveCodes$dem2Category == "Ideology ") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem3Category) == T, F,  wideFromLongDemAdjectiveCodes$dem3Category == "Ideology ") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem4Category) == T, F,  wideFromLongDemAdjectiveCodes$dem4Category == "Ideology ")

wideFromLongDemAdjectiveCodes$Symbol_number <- ifelse(is.na(wideFromLongDemAdjectiveCodes$dem1Category) == T, F,  wideFromLongDemAdjectiveCodes$dem1Category == "Symbols and Party Leaders") + 
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem2Category) == T, F,  wideFromLongDemAdjectiveCodes$dem2Category == "Symbols and Party Leaders") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem3Category) == T, F,  wideFromLongDemAdjectiveCodes$dem3Category == "Symbols and Party Leaders") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem4Category) == T, F,  wideFromLongDemAdjectiveCodes$dem4Category == "Symbols and Party Leaders")

wideFromLongDemAdjectiveCodes$Demographics_number <- ifelse(is.na(wideFromLongDemAdjectiveCodes$dem1Category) == T, F,  wideFromLongDemAdjectiveCodes$dem1Category == "Demographics ") + 
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem2Category) == T, F,  wideFromLongDemAdjectiveCodes$dem2Category == "Demographics ") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem3Category) == T, F,  wideFromLongDemAdjectiveCodes$dem3Category == "Demographics ") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem4Category) == T, F,  wideFromLongDemAdjectiveCodes$dem4Category == "Demographics ")

wideFromLongDemAdjectiveCodes$Issues_number <- ifelse(is.na(wideFromLongDemAdjectiveCodes$dem1Category) == T, F,  wideFromLongDemAdjectiveCodes$dem1Category == "Issues") + 
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem2Category) == T, F,  wideFromLongDemAdjectiveCodes$dem2Category == "Issues") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem3Category) == T, F,  wideFromLongDemAdjectiveCodes$dem3Category == "Issues") +
  ifelse(is.na(wideFromLongDemAdjectiveCodes$dem4Category) == T, F,  wideFromLongDemAdjectiveCodes$dem4Category == "Issues")

wideFromLongDemAdjectiveCodes$NA_number <- is.na(wideFromLongDemAdjectiveCodes$dem1Category) + is.na(wideFromLongDemAdjectiveCodes$dem2Category) + is.na(wideFromLongDemAdjectiveCodes$dem3Category) + is.na(wideFromLongDemAdjectiveCodes$dem4Category)


wideFromLongDemAdjectiveCodes$PT_type <- 0
wideFromLongDemAdjectiveCodes$Value_type <- 0
wideFromLongDemAdjectiveCodes$Ideology_type <- 0
wideFromLongDemAdjectiveCodes$Symbol_type <- 0
wideFromLongDemAdjectiveCodes$Demographics_type <- 0
wideFromLongDemAdjectiveCodes$Issues_type <- 0
wideFromLongDemAdjectiveCodes$NA_type <- 0
wideFromLongDemAdjectiveCodes$tie <- 0


for(i in 1:length(wideFromLongDemAdjectiveCodes$PT_type)) {
  max <- max(c(wideFromLongDemAdjectiveCodes$PT_number[i], wideFromLongDemAdjectiveCodes$Value_number[i], wideFromLongDemAdjectiveCodes$Ideology_number[i], wideFromLongDemAdjectiveCodes$Symbol_number[i], wideFromLongDemAdjectiveCodes$Demographics_number[i], wideFromLongDemAdjectiveCodes$Issues_number[i]))
  if(max > 0) {
    if(wideFromLongDemAdjectiveCodes$PT_number[i] == max) {wideFromLongDemAdjectiveCodes$PT_type[i] <- 1}
    if(wideFromLongDemAdjectiveCodes$Value_number[i] == max) {wideFromLongDemAdjectiveCodes$Value_type[i] <- 1}
    if(wideFromLongDemAdjectiveCodes$Ideology_number[i] == max) {wideFromLongDemAdjectiveCodes$Ideology_type[i] <- 1}
    if(wideFromLongDemAdjectiveCodes$Symbol_number[i] == max) {wideFromLongDemAdjectiveCodes$Symbol_type[i] <- 1}
    if(wideFromLongDemAdjectiveCodes$Demographics_number[i] == max) {wideFromLongDemAdjectiveCodes$Demographics_type[i] <- 1}
    if(wideFromLongDemAdjectiveCodes$Issues_number[i] == max) {wideFromLongDemAdjectiveCodes$Issues_type[i] <- 1}
    if(sum(c(wideFromLongDemAdjectiveCodes$PT_number[i], wideFromLongDemAdjectiveCodes$Value_number[i], wideFromLongDemAdjectiveCodes$Ideology_number[i], wideFromLongDemAdjectiveCodes$Symbol_number[i], wideFromLongDemAdjectiveCodes$Demographics_number[i], wideFromLongDemAdjectiveCodes$Issues_number[i]) == max) > 1) {wideFromLongDemAdjectiveCodes$tie[i] <- 1}
  }
  if(max == 0) {wideFromLongDemAdjectiveCodes$NA_type[i] <- 1}
}


##Proportions and difference in proportion tests reported in Table A8

#First, the tests for the full sample

typePTtest_demAll <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$PT_type[which(wideFromLongDemAdjectiveCodes$sample == 1)]), sum(wideFromLongDemAdjectiveCodes$PT_type[which(wideFromLongDemAdjectiveCodes$sample == 2)])), c(988, 1452))

typeIdeologytest_demAll <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Ideology_type[which(wideFromLongDemAdjectiveCodes$sample == 1)]), sum(wideFromLongDemAdjectiveCodes$Ideology_type[which(wideFromLongDemAdjectiveCodes$sample == 2)])), c(988, 1452))

typeDemographicstest_demAll <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Demographics_type[which(wideFromLongDemAdjectiveCodes$sample == 1)]), sum(wideFromLongDemAdjectiveCodes$Demographics_type[which(wideFromLongDemAdjectiveCodes$sample == 2)])), c(988, 1452))

typeValuetest_demAll <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Value_type[which(wideFromLongDemAdjectiveCodes$sample == 1)]), sum(wideFromLongDemAdjectiveCodes$Value_type[which(wideFromLongDemAdjectiveCodes$sample == 2)])), c(988, 1452))

typeIssuestest_demAll <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Issues_type[which(wideFromLongDemAdjectiveCodes$sample == 1)]), sum(wideFromLongDemAdjectiveCodes$Issues_type[which(wideFromLongDemAdjectiveCodes$sample == 2)])), c(988, 1452))

typeSymboltest_demAll <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Symbol_type[which(wideFromLongDemAdjectiveCodes$sample == 1)]), sum(wideFromLongDemAdjectiveCodes$Symbol_type[which(wideFromLongDemAdjectiveCodes$sample == 2)])), c(988, 1452))

typeNAtest_demAll <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$NA_type[which(wideFromLongDemAdjectiveCodes$sample == 1)]), sum(wideFromLongDemAdjectiveCodes$NA_type[which(wideFromLongDemAdjectiveCodes$sample == 2)])), c(988, 1452))

#Next, the tests for the subsample of Democratic respondents

typePTtest_demDems <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$PT_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)]), sum(wideFromLongDemAdjectiveCodes$PT_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeIdeologytest_demDems <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Ideology_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)]), sum(wideFromLongDemAdjectiveCodes$Ideology_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeDemographicstest_demDems <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Demographics_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)]), sum(wideFromLongDemAdjectiveCodes$Demographics_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeValuetest_demDems <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Value_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)]), sum(wideFromLongDemAdjectiveCodes$Value_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeIssuestest_demDems <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Issues_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)]), sum(wideFromLongDemAdjectiveCodes$Issues_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeSymboltest_demDems <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Symbol_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)]), sum(wideFromLongDemAdjectiveCodes$Symbol_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeNAtest_demDems <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$NA_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 1)]), sum(wideFromLongDemAdjectiveCodes$NA_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 1)])), c(446, 719))

#Next, the tests for the subsample of Republicans

typePTtest_demReps <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$PT_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)]), sum(wideFromLongDemAdjectiveCodes$PT_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeIdeologytest_demReps <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Ideology_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)]), sum(wideFromLongDemAdjectiveCodes$Ideology_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeDemographicstest_demReps <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Demographics_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)]), sum(wideFromLongDemAdjectiveCodes$Demographics_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeValuetest_demReps <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Value_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)]), sum(wideFromLongDemAdjectiveCodes$Value_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeIssuestest_demReps <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Issues_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)]), sum(wideFromLongDemAdjectiveCodes$Issues_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeSymboltest_demReps <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$Symbol_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)]), sum(wideFromLongDemAdjectiveCodes$Symbol_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeNAtest_demReps <- prop.test(c(sum(wideFromLongDemAdjectiveCodes$NA_type[which(wideFromLongDemAdjectiveCodes$sample == 1 & wideFromLongDemAdjectiveCodes$PID3 == 3)]), sum(wideFromLongDemAdjectiveCodes$NA_type[which(wideFromLongDemAdjectiveCodes$sample == 2 & wideFromLongDemAdjectiveCodes$PID3 == 3)])), c(363, 435))

#Now proportion of respondents using at least one word from the category to describe Republicans

repCategoriesCount <- rep(0,length(repCategories))
repCategoriesCountSample1 <- rep(0,length(repCategories))
repCategoriesCountSample2 <- rep(0,length(repCategories))
repCategoriesCountSample1Dems <- rep(0,length(repCategories))
repCategoriesCountSample2Dems <- rep(0,length(repCategories))
repCategoriesCountSample1Reps <- rep(0,length(repCategories))
repCategoriesCountSample2Reps <- rep(0,length(repCategories))

for (i in 1:length(repCategories)) {
  repCategoriesCount[i] <- sum(wideFromLongRepAdjectiveCodes$rep1Category == repCategories[i] | wideFromLongRepAdjectiveCodes$rep2Category == repCategories[i] | wideFromLongRepAdjectiveCodes$rep3Category == repCategories[i] | wideFromLongRepAdjectiveCodes$rep4Category == repCategories[i], na.rm = T)
  
  repCategoriesCountSample1[i] <- sum(wideFromLongRepAdjectiveCodes$rep1Category[which(wideFromLongRepAdjectiveCodes$sample == 1)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep2Category[which(wideFromLongRepAdjectiveCodes$sample == 1)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep3Category[which(wideFromLongRepAdjectiveCodes$sample == 1)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep4Category[which(wideFromLongRepAdjectiveCodes$sample == 1)] == repCategories[i],na.rm = T)
  
  repCategoriesCountSample2[i] <- sum(wideFromLongRepAdjectiveCodes$rep1Category[which(wideFromLongRepAdjectiveCodes$sample == 2)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep2Category[which(wideFromLongRepAdjectiveCodes$sample == 2)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep3Category[which(wideFromLongRepAdjectiveCodes$sample == 2)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep4Category[which(wideFromLongRepAdjectiveCodes$sample == 2)] == repCategories[i], na.rm = T)
  
  repCategoriesCountSample1Dems[i] <- sum(wideFromLongRepAdjectiveCodes$rep1Category[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep2Category[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep3Category[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep4Category[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)] == repCategories[i], na.rm = T)
  
  repCategoriesCountSample2Dems[i] <- sum(wideFromLongRepAdjectiveCodes$rep1Category[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep2Category[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep3Category[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep4Category[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)] == repCategories[i], na.rm = T)
  
  repCategoriesCountSample1Reps[i] <- sum(wideFromLongRepAdjectiveCodes$rep1Category[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep2Category[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep3Category[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep4Category[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)] == repCategories[i], na.rm = T)
  
  repCategoriesCountSample2Reps[i] <- sum(wideFromLongRepAdjectiveCodes$rep1Category[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep2Category[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep3Category[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)] == repCategories[i] | wideFromLongRepAdjectiveCodes$rep4Category[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)] == repCategories[i], na.rm = T)
}

categoriesOfReps <- data.frame(repCategories, repCategoriesCount, repCategoriesCountSample1, repCategoriesCountSample2, repCategoriesCountSample1Dems, repCategoriesCountSample2Dems, repCategoriesCountSample1Reps, repCategoriesCountSample2Reps)

categoriesOfReps$prop.test_All <- 1
categoriesOfReps$prop.test_Dems <- 1
categoriesOfReps$prop.test_Reps <- 1


for (i in c(1:length(categoriesOfReps$repCategories))) {
  categoriesOfReps$prop.test_All[i] <- prop.test(c(categoriesOfReps$repCategoriesCountSample1[i], categoriesOfReps$repCategoriesCountSample2[i]), c(length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 1)]), length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 2)])))$p.value
}

for (i in c(1:length(categoriesOfReps$repCategories))) {
  categoriesOfReps$prop.test_Dems[i] <- prop.test(c(categoriesOfReps$repCategoriesCountSample1Dems[i], categoriesOfReps$repCategoriesCountSample2Dems[i]), c(length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)]), length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)])))$p.value
}

for (i in c(1:length(categoriesOfReps$repCategories))) {
  categoriesOfReps$prop.test_Reps[i] <- prop.test(c(categoriesOfReps$repCategoriesCountSample1Reps[i], categoriesOfReps$repCategoriesCountSample2Reps[i]), c(length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)]), length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)])))$p.value
}

categoriesOfReps$sample2N <-length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 2 )])
categoriesOfReps$sample1N <-length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 1)])
categoriesOfReps$demSample2N <-length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)])
categoriesOfReps$demSample1N <-length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)])
categoriesOfReps$repSample2N <-length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)])
categoriesOfReps$repSample1N <-length(wideFromLongRepAdjectiveCodes$sample[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)])

categoriesOfReps$sample1Prop <- categoriesOfReps$repCategoriesCountSample1/categoriesOfReps$sample1N
categoriesOfReps$sample2Prop <- categoriesOfReps$repCategoriesCountSample2/categoriesOfReps$sample2N
categoriesOfReps$demSample1Prop <- categoriesOfReps$repCategoriesCountSample1Dems/categoriesOfReps$demSample1N
categoriesOfReps$demSmple2Prop <- categoriesOfReps$repCategoriesCountSample2Dems/categoriesOfReps$demSample2N
categoriesOfReps$repSample1Prop <- categoriesOfReps$repCategoriesCountSample1Reps/categoriesOfReps$repSample1N
categoriesOfReps$repSample2Prop <- categoriesOfReps$repCategoriesCountSample2Reps/categoriesOfReps$repSample2N


categoriesOfReps$sig <- ifelse((categoriesOfReps$prop.test_All < .05 | categoriesOfReps$prop.test_Dems < .05 | categoriesOfReps$prop.test_Reps < .05),1,0)

write.csv(categoriesOfReps, "categoriesOfReps.csv", row.names = FALSE)


#Now categorizing respondents' `type' by their most used kind of descriptor

wideFromLongRepAdjectiveCodes$PT_number <- ifelse(is.na(wideFromLongRepAdjectiveCodes$rep1Category) == T, F,  wideFromLongRepAdjectiveCodes$rep1Category == "Personal trait ") + 
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep2Category) == T, F,  wideFromLongRepAdjectiveCodes$rep2Category == "Personal trait ") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep3Category) == T, F,  wideFromLongRepAdjectiveCodes$rep3Category == "Personal trait ") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep4Category) == T, F,  wideFromLongRepAdjectiveCodes$rep4Category == "Personal trait ")

wideFromLongRepAdjectiveCodes$Value_number <- ifelse(is.na(wideFromLongRepAdjectiveCodes$rep1Category) == T, F,  wideFromLongRepAdjectiveCodes$rep1Category == "Value") + 
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep2Category) == T, F,  wideFromLongRepAdjectiveCodes$rep2Category == "Value") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep3Category) == T, F,  wideFromLongRepAdjectiveCodes$rep3Category == "Value") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep4Category) == T, F,  wideFromLongRepAdjectiveCodes$rep4Category == "Value")

wideFromLongRepAdjectiveCodes$Ideology_number <- ifelse(is.na(wideFromLongRepAdjectiveCodes$rep1Category) == T, F,  wideFromLongRepAdjectiveCodes$rep1Category == "Ideology ") + 
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep2Category) == T, F,  wideFromLongRepAdjectiveCodes$rep2Category == "Ideology ") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep3Category) == T, F,  wideFromLongRepAdjectiveCodes$rep3Category == "Ideology ") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep4Category) == T, F,  wideFromLongRepAdjectiveCodes$rep4Category == "Ideology ")

wideFromLongRepAdjectiveCodes$Symbol_number <- ifelse(is.na(wideFromLongRepAdjectiveCodes$rep1Category) == T, F,  wideFromLongRepAdjectiveCodes$rep1Category == "Symbols and Party Leaders") + 
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep2Category) == T, F,  wideFromLongRepAdjectiveCodes$rep2Category == "Symbols and Party Leaders") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep3Category) == T, F,  wideFromLongRepAdjectiveCodes$rep3Category == "Symbols and Party Leaders") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep4Category) == T, F,  wideFromLongRepAdjectiveCodes$rep4Category == "Symbols and Party Leaders")

wideFromLongRepAdjectiveCodes$Demographics_number <- ifelse(is.na(wideFromLongRepAdjectiveCodes$rep1Category) == T, F,  wideFromLongRepAdjectiveCodes$rep1Category == "Demographics ") + 
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep2Category) == T, F,  wideFromLongRepAdjectiveCodes$rep2Category == "Demographics ") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep3Category) == T, F,  wideFromLongRepAdjectiveCodes$rep3Category == "Demographics ") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep4Category) == T, F,  wideFromLongRepAdjectiveCodes$rep4Category == "Demographics ")

wideFromLongRepAdjectiveCodes$Issues_number <- ifelse(is.na(wideFromLongRepAdjectiveCodes$rep1Category) == T, F,  wideFromLongRepAdjectiveCodes$rep1Category == "Issues") + 
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep2Category) == T, F,  wideFromLongRepAdjectiveCodes$rep2Category == "Issues") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep3Category) == T, F,  wideFromLongRepAdjectiveCodes$rep3Category == "Issues") +
  ifelse(is.na(wideFromLongRepAdjectiveCodes$rep4Category) == T, F,  wideFromLongRepAdjectiveCodes$rep4Category == "Issues")

wideFromLongRepAdjectiveCodes$NA_number <- is.na(wideFromLongRepAdjectiveCodes$rep1Category) + is.na(wideFromLongRepAdjectiveCodes$rep2Category) + is.na(wideFromLongRepAdjectiveCodes$rep3Category) + is.na(wideFromLongRepAdjectiveCodes$rep4Category)


wideFromLongRepAdjectiveCodes$PT_type <- 0
wideFromLongRepAdjectiveCodes$Value_type <- 0
wideFromLongRepAdjectiveCodes$Ideology_type <- 0
wideFromLongRepAdjectiveCodes$Symbol_type <- 0
wideFromLongRepAdjectiveCodes$Demographics_type <- 0
wideFromLongRepAdjectiveCodes$Issues_type <- 0
wideFromLongRepAdjectiveCodes$NA_type <- 0
wideFromLongRepAdjectiveCodes$tie <- 0


for(i in 1:length(wideFromLongRepAdjectiveCodes$PT_type)) {
  max <- max(c(wideFromLongRepAdjectiveCodes$PT_number[i], wideFromLongRepAdjectiveCodes$Value_number[i], wideFromLongRepAdjectiveCodes$Ideology_number[i], wideFromLongRepAdjectiveCodes$Symbol_number[i], wideFromLongRepAdjectiveCodes$Demographics_number[i], wideFromLongRepAdjectiveCodes$Issues_number[i]))
  if(max > 0) {
    if(wideFromLongRepAdjectiveCodes$PT_number[i] == max) {wideFromLongRepAdjectiveCodes$PT_type[i] <- 1}
    if(wideFromLongRepAdjectiveCodes$Value_number[i] == max) {wideFromLongRepAdjectiveCodes$Value_type[i] <- 1}
    if(wideFromLongRepAdjectiveCodes$Ideology_number[i] == max) {wideFromLongRepAdjectiveCodes$Ideology_type[i] <- 1}
    if(wideFromLongRepAdjectiveCodes$Symbol_number[i] == max) {wideFromLongRepAdjectiveCodes$Symbol_type[i] <- 1}
    if(wideFromLongRepAdjectiveCodes$Demographics_number[i] == max) {wideFromLongRepAdjectiveCodes$Demographics_type[i] <- 1}
    if(wideFromLongRepAdjectiveCodes$Issues_number[i] == max) {wideFromLongRepAdjectiveCodes$Issues_type[i] <- 1}
    if(sum(c(wideFromLongRepAdjectiveCodes$PT_number[i], wideFromLongRepAdjectiveCodes$Value_number[i], wideFromLongRepAdjectiveCodes$Ideology_number[i], wideFromLongRepAdjectiveCodes$Symbol_number[i], wideFromLongRepAdjectiveCodes$Demographics_number[i], wideFromLongRepAdjectiveCodes$Issues_number[i]) == max) > 1) {wideFromLongRepAdjectiveCodes$tie[i] <- 1}
  }
  if(max == 0) {wideFromLongRepAdjectiveCodes$NA_type[i] <- 1}
}


##Proportions and difference in proportion tests reported in Table A9

#First, the tests for the full sample

typePTtest_repAll <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$PT_type[which(wideFromLongRepAdjectiveCodes$sample == 1)]), sum(wideFromLongRepAdjectiveCodes$PT_type[which(wideFromLongRepAdjectiveCodes$sample == 2)])), c(988, 1452))

typeIdeologytest_repAll <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Ideology_type[which(wideFromLongRepAdjectiveCodes$sample == 1)]), sum(wideFromLongRepAdjectiveCodes$Ideology_type[which(wideFromLongRepAdjectiveCodes$sample == 2)])), c(988, 1452))

typeDemographicstest_repAll <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Demographics_type[which(wideFromLongRepAdjectiveCodes$sample == 1)]), sum(wideFromLongRepAdjectiveCodes$Demographics_type[which(wideFromLongRepAdjectiveCodes$sample == 2)])), c(988, 1452))

typeValuetest_repAll <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Value_type[which(wideFromLongRepAdjectiveCodes$sample == 1)]), sum(wideFromLongRepAdjectiveCodes$Value_type[which(wideFromLongRepAdjectiveCodes$sample == 2)])), c(988, 1452))

typeIssuestest_repAll <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Issues_type[which(wideFromLongRepAdjectiveCodes$sample == 1)]), sum(wideFromLongRepAdjectiveCodes$Issues_type[which(wideFromLongRepAdjectiveCodes$sample == 2)])), c(988, 1452))

typeSymboltest_repAll <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Symbol_type[which(wideFromLongRepAdjectiveCodes$sample == 1)]), sum(wideFromLongRepAdjectiveCodes$Symbol_type[which(wideFromLongRepAdjectiveCodes$sample == 2)])), c(988, 1452))

typeNAtest_repAll <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$NA_type[which(wideFromLongRepAdjectiveCodes$sample == 1)]), sum(wideFromLongRepAdjectiveCodes$NA_type[which(wideFromLongRepAdjectiveCodes$sample == 2)])), c(988, 1452))

#Next, the tests for the subsample of Democratic respondents

typePTtest_repDems <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$PT_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)]), sum(wideFromLongRepAdjectiveCodes$PT_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeIdeologytest_repDems <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Ideology_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)]), sum(wideFromLongRepAdjectiveCodes$Ideology_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeDemographicstest_repDems <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Demographics_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)]), sum(wideFromLongRepAdjectiveCodes$Demographics_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeValuetest_repDems <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Value_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)]), sum(wideFromLongRepAdjectiveCodes$Value_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeIssuestest_repDems <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Issues_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)]), sum(wideFromLongRepAdjectiveCodes$Issues_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeSymboltest_repDems <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Symbol_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)]), sum(wideFromLongRepAdjectiveCodes$Symbol_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)])), c(446, 719))

typeNAtest_repDems <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$NA_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 1)]), sum(wideFromLongRepAdjectiveCodes$NA_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 1)])), c(446, 719))

#Next, the tests for the subsample of Republicans

typePTtest_repReps <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$PT_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)]), sum(wideFromLongRepAdjectiveCodes$PT_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeIdeologytest_repReps <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Ideology_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)]), sum(wideFromLongRepAdjectiveCodes$Ideology_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeDemographicstest_repReps <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Demographics_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)]), sum(wideFromLongRepAdjectiveCodes$Demographics_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeValuetest_repReps <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Value_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)]), sum(wideFromLongRepAdjectiveCodes$Value_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeIssuestest_repReps <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Issues_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)]), sum(wideFromLongRepAdjectiveCodes$Issues_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeSymboltest_repReps <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$Symbol_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)]), sum(wideFromLongRepAdjectiveCodes$Symbol_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)])), c(363, 435))

typeNAtest_repReps <- prop.test(c(sum(wideFromLongRepAdjectiveCodes$NA_type[which(wideFromLongRepAdjectiveCodes$sample == 1 & wideFromLongRepAdjectiveCodes$PID3 == 3)]), sum(wideFromLongRepAdjectiveCodes$NA_type[which(wideFromLongRepAdjectiveCodes$sample == 2 & wideFromLongRepAdjectiveCodes$PID3 == 3)])), c(363, 435))

##Finally, adding in valence coding

wideFromLongDem$meanVal <- NA

for (i in 1:length(wideFromLongDem$meanVal)) {
  wideFromLongDem$meanVal[i] <- mean(c(wideFromLongDem$dem1_v[i], wideFromLongDem$dem2_v[i], wideFromLongDem$dem3_v[i], wideFromLongDem$dem4_v[i]), na.rm = T)
}

mean(wideFromLongDem$meanVal, na.rm = T)
mean(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 1)], na.rm = T)
mean(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 2)], na.rm = T)

t.test(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 1)], wideFromLongDem$meanVal[which(wideFromLongDem$sample == 2)], na.rm = T)
t.test(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 1)], na.rm = T)
t.test(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 2)], na.rm = T)

t.test(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 1)], wideFromLongDem$meanVal[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 1)], na.rm = T)
t.test(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 1)], na.rm = T)
t.test(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 1)], na.rm = T)

t.test(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 3)], wideFromLongDem$meanVal[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 3)], na.rm = T)
t.test(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 1 & wideFromLongDem$PID3 == 3)], na.rm = T)
t.test(wideFromLongDem$meanVal[which(wideFromLongDem$sample == 2 & wideFromLongDem$PID3 == 3)], na.rm = T)


wideFromLongRep$meanVal <- NA

for (i in 1:length(wideFromLongRep$meanVal)) {
  wideFromLongRep$meanVal[i] <- mean(c(wideFromLongRep$rep1_v[i], wideFromLongRep$rep2_v[i], wideFromLongRep$rep3_v[i], wideFromLongRep$rep4_v[i]), na.rm = T)
}

mean(wideFromLongRep$meanVal, na.rm = T)
mean(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 1)], na.rm = T)
mean(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 2)], na.rm = T)

t.test(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 1)], wideFromLongRep$meanVal[which(wideFromLongRep$sample == 2)], na.rm = T)
t.test(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 1)], na.rm = T)
t.test(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 2)], na.rm = T)

t.test(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 1)], wideFromLongRep$meanVal[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 1)], na.rm = T)
t.test(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 1)], na.rm = T)
t.test(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 1)], na.rm = T)

t.test(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 3)], wideFromLongRep$meanVal[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 3)], na.rm = T)
t.test(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 1 & wideFromLongRep$PID3 == 3)], na.rm = T)
t.test(wideFromLongRep$meanVal[which(wideFromLongRep$sample == 2 & wideFromLongRep$PID3 == 3)], na.rm = T)


##Creating subsets of the larger table for the tables that appear in the appendix

tableA1dem2016 <- as.data.frame(cbind(descriptorsOfDems$demDescriptors, descriptorsOfDems$sample1Prop))
colnames(tableA1dem2016) <- c("Descriptor", "Dem2016")
tableA1dem2016 <- tableA1dem2016[order(tableA1dem2016$Dem2016, decreasing = T),]

tableA1dem2021 <- as.data.frame(cbind(descriptorsOfDems$demDescriptors, descriptorsOfDems$sample2Prop))
colnames(tableA1dem2021) <- c("Descriptor", "Dem2021")
tableA1dem2021 <- tableA1dem2021[order(tableA1dem2021$Dem2021, decreasing = T),]

tableA1rep2016 <- as.data.frame(cbind(descriptorsOfReps$repDescriptors, descriptorsOfReps$sample1Prop))
colnames(tableA1rep2016) <- c("Descriptor", "Rep2016")
tableA1rep2016 <- tableA1rep2016[order(tableA1rep2016$Rep2016, decreasing = T),]

tableA1rep2021 <- as.data.frame(cbind(descriptorsOfReps$repDescriptors, descriptorsOfReps$sample2Prop))
colnames(tableA1rep2021) <- c("Descriptor", "Rep2021")
tableA1rep2021 <- tableA1rep2021[order(tableA1rep2021$Rep2021, decreasing = T),]

tableA1 <- cbind(tableA1dem2016[1:10,], tableA1dem2021[1:10,], tableA1rep2016[1:10,], tableA1rep2021[1:10,])


tableA2dem2016 <- as.data.frame(cbind(descriptorsOfDems$demDescriptors, descriptorsOfDems$demSample1Prop))
colnames(tableA2dem2016) <- c("Descriptor", "Dem2016")
tableA2dem2016 <- tableA2dem2016[order(tableA2dem2016$Dem2016, decreasing = T),]

tableA2dem2021 <- as.data.frame(cbind(descriptorsOfDems$demDescriptors, descriptorsOfDems$demSmple2Prop))
colnames(tableA2dem2021) <- c("Descriptor", "Dem2021")
tableA2dem2021 <- tableA2dem2021[order(tableA2dem2021$Dem2021, decreasing = T),]

tableA2rep2016 <- as.data.frame(cbind(descriptorsOfReps$repDescriptors, descriptorsOfReps$demSample1Prop))
colnames(tableA2rep2016) <- c("Descriptor", "Rep2016")
tableA2rep2016 <- tableA2rep2016[order(tableA2rep2016$Rep2016, decreasing = T),]

tableA2rep2021 <- as.data.frame(cbind(descriptorsOfReps$repDescriptors, descriptorsOfReps$demSmple2Prop))
colnames(tableA2rep2021) <- c("Descriptor", "Rep2021")
tableA2rep2021 <- tableA2rep2021[order(tableA2rep2021$Rep2021, decreasing = T),]

tableA2 <- cbind(tableA2dem2016[1:10,], tableA2dem2021[1:10,], tableA2rep2016[1:10,], tableA2rep2021[1:10,])



tableA3dem2016 <- as.data.frame(cbind(descriptorsOfDems$demDescriptors, descriptorsOfDems$repSample1Prop))
colnames(tableA3dem2016) <- c("Descriptor", "Dem2016")
tableA3dem2016 <- tableA3dem2016[order(tableA3dem2016$Dem2016, decreasing = T),]

tableA3dem2021 <- as.data.frame(cbind(descriptorsOfDems$demDescriptors, descriptorsOfDems$repSample2Prop))
colnames(tableA3dem2021) <- c("Descriptor", "Dem2021")
tableA3dem2021 <- tableA3dem2021[order(tableA3dem2021$Dem2021, decreasing = T),]

tableA3rep2016 <- as.data.frame(cbind(descriptorsOfReps$repDescriptors, descriptorsOfReps$repSample1Prop))
colnames(tableA3rep2016) <- c("Descriptor", "Rep2016")
tableA3rep2016 <- tableA3rep2016[order(tableA3rep2016$Rep2016, decreasing = T),]

tableA3rep2021 <- as.data.frame(cbind(descriptorsOfReps$repDescriptors, descriptorsOfReps$repSample2Prop))
colnames(tableA3rep2021) <- c("Descriptor", "Rep2021")
tableA3rep2021 <- tableA3rep2021[order(tableA3rep2021$Rep2021, decreasing = T),]

tableA3 <- cbind(tableA3dem2016[1:10,], tableA3dem2021[1:10,], tableA3rep2016[1:10,], tableA3rep2021[1:10,])

tableA4 <- descriptorsOfDems[which(descriptorsOfDems$sig == 1),]
tableA4 <- tableA4[,c(1,9:11,18:23)]
tableA4 <- tableA4[,c(1,5,6,2,7,8,3,9,10,4)]

tableA5 <- descriptorsOfReps[which(descriptorsOfReps$sig == 1),]
tableA5 <- tableA5[,c(1,9:11,18:23)]
tableA5 <- tableA5[,c(1,5,6,2,7,8,3,9,10,4)]

tableA6 <- categoriesOfDems[2:7,]
tableA6 <- tableA6[,c(1,9:11,18:23)]
tableA6 <- tableA6[,c(1,5,6,2,7,8,3,9,10,4)]
tableA6 <- tableA6[order(tableA6$sample1Prop, decreasing = T),]

tableA7 <- categoriesOfReps[2:7,]
tableA7 <- tableA7[,c(1,9:11,18:23)]
tableA7 <- tableA7[,c(1,5,6,2,7,8,3,9,10,4)]
tableA7 <- tableA7[order(tableA7$sample1Prop, decreasing = T),]


tableA8 <- data.frame(c("Personal Traits", "Ideology", "Demo Groups", "Values", "Issues", "Symbols/Leaders", "NA"), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7))

colnames(tableA8) <- c("Category", "2016-All", "2021-All", "p", "2016-Dems", "2021-Dems", "p", "2016-Reps", "2021-Reps", "p")

tableA8[1,2:10] <- c(typePTtest_demAll$estimate, typePTtest_demAll$p.value,typePTtest_demDems$estimate, typePTtest_demDems$p.value,typePTtest_demReps$estimate, typePTtest_demReps$p.value)

tableA8[2,2:10] <- c(typeIdeologytest_demAll$estimate, typeIdeologytest_demAll$p.value,typeIdeologytest_demDems$estimate, typeIdeologytest_demDems$p.value,typeIdeologytest_demReps$estimate, typeIdeologytest_demReps$p.value)

tableA8[3,2:10] <- c(typeDemographicstest_demAll$estimate, typeDemographicstest_demAll$p.value,typeDemographicstest_demDems$estimate, typeDemographicstest_demDems$p.value,typeDemographicstest_demReps$estimate, typeDemographicstest_demReps$p.value)

tableA8[4,2:10] <- c(typeValuetest_demAll$estimate, typeValuetest_demAll$p.value,typeValuetest_demDems$estimate, typeValuetest_demDems$p.value,typeValuetest_demReps$estimate, typeValuetest_demReps$p.value)

tableA8[5,2:10] <- c(typeIssuestest_demAll$estimate, typeIssuestest_demAll$p.value,typeIssuestest_demDems$estimate, typeIssuestest_demDems$p.value,typeIssuestest_demReps$estimate, typeIssuestest_demReps$p.value)

tableA8[6,2:10] <- c(typeSymboltest_demAll$estimate, typeSymboltest_demAll$p.value,typeSymboltest_demDems$estimate, typeSymboltest_demDems$p.value,typeSymboltest_demReps$estimate, typeSymboltest_demReps$p.value)

tableA8[7,2:10] <- c(typeNAtest_demAll$estimate, typeNAtest_demAll$p.value,typeNAtest_demDems$estimate, typeNAtest_demDems$p.value,typeNAtest_demReps$estimate, typeNAtest_demReps$p.value)


tableA9 <- data.frame(c("Personal Traits", "Ideology", "Demo Groups", "Values", "Issues", "Symbols/Leaders", "NA"), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7), numeric(7))

colnames(tableA9) <- c("Category", "2016-All", "2021-All", "p", "2016-Dems", "2021-Dems", "p", "2016-Reps", "2021-Reps", "p")

tableA9[1,2:10] <- c(typePTtest_repAll$estimate, typePTtest_repAll$p.value,typePTtest_repDems$estimate, typePTtest_repDems$p.value,typePTtest_repReps$estimate, typePTtest_repReps$p.value)

tableA9[2,2:10] <- c(typeIdeologytest_repAll$estimate, typeIdeologytest_repAll$p.value,typeIdeologytest_repDems$estimate, typeIdeologytest_repDems$p.value,typeIdeologytest_repReps$estimate, typeIdeologytest_repReps$p.value)

tableA9[3,2:10] <- c(typeDemographicstest_repAll$estimate, typeDemographicstest_repAll$p.value,typeDemographicstest_repDems$estimate, typeDemographicstest_repDems$p.value,typeDemographicstest_repReps$estimate, typeDemographicstest_repReps$p.value)

tableA9[4,2:10] <- c(typeValuetest_repAll$estimate, typeValuetest_repAll$p.value,typeValuetest_repDems$estimate, typeValuetest_repDems$p.value,typeValuetest_repReps$estimate, typeValuetest_repReps$p.value)

tableA9[5,2:10] <- c(typeIssuestest_repAll$estimate, typeIssuestest_repAll$p.value,typeIssuestest_repDems$estimate, typeIssuestest_repDems$p.value,typeIssuestest_repReps$estimate, typeIssuestest_repReps$p.value)

tableA9[6,2:10] <- c(typeSymboltest_repAll$estimate, typeSymboltest_repAll$p.value,typeSymboltest_repDems$estimate, typeSymboltest_repDems$p.value,typeSymboltest_repReps$estimate, typeSymboltest_repReps$p.value)

tableA9[7,2:10] <- c(typeNAtest_repAll$estimate, typeNAtest_repAll$p.value,typeNAtest_repDems$estimate, typeNAtest_repDems$p.value,typeNAtest_repReps$estimate, typeNAtest_repReps$p.value)
